-- ghost_ship
-- created by panyl
-- 地牢格子：幽灵船

return {
    restoreGrid = function(grid, info)
        -- TODO: 还原状态
        local ship = grid.ship or grid.monster;

        -- 属性还原
        if info["ghost_ship"]["prop"] then
            local prop = info["ghost_ship"]["prop"];
            ship.dbase:setTemp("prop", prop);
            -- 整理下
            PropM.arrangeProps(ship);
        end

        local skills = info["ghost_ship"]["skills"];
        if info["ghost_ship"]["skills"] then
            if skills.array then
                info["ghost_ship"]["skills"] = skills:toTable();
                ship:setSkills(skills:toTable());
            else
                info["ghost_ship"]["skills"] = skills;
                ship:setSkills(skills);
            end
        end

        if info["ghost_ship"]["action"] then
            ship.action = info["ghost_ship"]["action"];
        end
        if info["ghost_ship"]["skill_round"] then
            ship.skill_round = info["ghost_ship"]["skill_round"];
        end

        ship.dbase:aborb(info["ghost_ship"]);

        ship:checkPauseBackPerformance();
    end,

    collectContext = function(grid)
        local ship = grid.ship or grid.monster;

        local ret = {
            ["class_id"] = ship.classId,
            ["level"] = ship.level,
            ["hp"] = ship:query("hp"),
            ["skills"] = ship:getAllSkills(),
            ["action"] = ship.action or 0,
            ["skill_round"] = ship.skill_round or 0,
            ["is_dead"]     = ship:query("is_dead"),
        };

        if ship:isHired() then
            ME.user.dbase:set("ghost_ship", ret);

            ret["hired"] = 1;
        else
            ret["prop"] = ship.dbase:queryTemp("prop");
        end

        return {["ghost_ship"] = ret, };
    end,

    autoFight = function(grid)
        -- 雇佣幽灵船
        if GhostShipM.canEmploy(grid:getPos()) then
            return DungeonActionM.go("employ_ship", grid:getPos());
        else
            return false;
        end
    end,
};
